package com.study.springboot.controller;

import com.study.springboot.common.mvc.BaseController;
import com.study.springboot.common.mvc.RespData;
import com.study.springboot.common.mvc.validate.PhoneValidate;
import com.study.springboot.common.mvc.validate.Save;
import com.study.springboot.common.mvc.validate.Update;
import com.study.springboot.common.mvc.validate.ValidationList;
import com.study.springboot.model.bo.StudentBO;
import com.study.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * @author wei.zhao
 * @description: 测试参数校验
 * @date 2021年12月25日 10:27
 */
@Validated
@RestController
@RequestMapping("/testValidate")
public class TestValidateController extends BaseController {

    @Autowired
    StudentService studentService;

    /* @RequestBody参数校验 */
    @PostMapping("/save")
    public RespData add(@RequestBody @Validated(value = {Save.class}) StudentBO studentBO) {
        return success();
    }

    /* @RequestParam、@PathVariable参数校验 */
    @GetMapping("/queryById/{id}/{age}")
    public RespData queryById(@PathVariable("id") @Min(0) @Max(100) Long id, @PathVariable("age") @Min(0) @Max(100) int age) {
        return success();
    }

    /* 分组校验、嵌套校验 */
    @PostMapping("/update")
    public RespData update(@RequestBody @Validated(Update.class) StudentBO studentBO) {
        return success();
    }

    /* 集合校验 */
    @PostMapping("/updateBatch")
    public RespData updateBatch(@RequestBody @Validated(Update.class) ValidationList<StudentBO> studentBOS) {
        return success();
    }

    /* 自定义手机号校验 */
    @GetMapping("/custom/{phone}")
    public RespData custom(@PathVariable("phone") @PhoneValidate String phone) {
        return success();
    }

}
